//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System.Collections;
using System.IO;
using CommunityServer.Components;

namespace CommunityServer.Galleries.Components
{
	/// <summary>
	/// Summary description for BaseWeblogRssWriter.
	/// </summary>
	public abstract class BaseGalleryRssWriter : BaseRssWriter
	{
        public BaseGalleryRssWriter(ArrayList posts, Section s, string baseUrl):base(posts,s,baseUrl)
        {
            
        }

		/// <summary>
		/// First looks for a custom rss.xsl in the theme directory; then defaults back to the 
		/// passed in path
		/// </summary>
		/// <param name="path"></param>
		protected override void WriteStyleSheet(string path)
		{
			string themePath = string.Format("{0}Galleries/{1}/rss.xsl", SiteUrls.Instance().Locations["themes"], CurrentGallery.Theme);

			if(File.Exists(CSContext.Current.Context.Server.MapPath(themePath)))
			{
				string styleSheetURL = Globals.FullPath(this.BaseUrl, themePath) ;
				this.WriteRaw(string.Format(styleElement,styleSheetURL)) ;
			}
			else
			{
				//#if !DEBUG
					base.WriteStyleSheet(path);
				//#endif
			}
		}

        protected override string EnclosureUrl(PostAttachmentMetaData pa)
        {
            if(pa.IsRemote)
                return pa.FileName;

           return this.BaseUrl + GalleryUrls.Instance().AttachmentUrl(this.CurrentGallery,pa);
        }

        protected Gallery CurrentGallery
        {
            get{ return CurrentSection as Gallery;}
        }

        protected override string BuildLink(Post p)
        {
            return GalleryUrls.Instance().ViewPicture(CurrentGallery.ApplicationKey, p as GalleryPost);
        }

        protected override string WritePubDate(Post p)
        {
			//Updated to user blogger time!
            return  ((GalleryPost)p).UserTime.ToUniversalTime().ToString("r");
        }

		protected override void WriteItem(Post p)
		{
			base.WriteItem (p);

			GalleryPost post = p as GalleryPost;
			if(post != null && post.HasCategories)
			{
				// Determine whether to write Categories or Tags to the RSS feed
				if (CurrentGallery.CategorizationType == CategorizationType.Album)
				{
					foreach(string name in post.Categories)
					{
						this.WriteStartElement("category");
						this.WriteAttributeString("domain", CategoryUrl(name));
						this.WriteString(name);
						this.WriteEndElement();
					}
				}
				else
				{
					foreach(string name in post.Categories)
					{
						this.WriteStartElement("category");
						this.WriteAttributeString("domain", TagUrl(name));
						this.WriteString(name);
						this.WriteEndElement();
					}
				}
			}
			
		}

		protected string CategoryUrl(string name)
		{
			PostCategory cat = PostCategories.GetCategory(name, this.CurrentGallery.SectionID); 
			return this.BaseUrl + GalleryUrls.Instance().ViewCategory(this.CurrentGallery.ApplicationKey, cat.CategoryID);
		}

		protected string TagUrl(string name)
		{
			return this.BaseUrl + GalleryUrls.Instance().TagsBrowser(CurrentGallery.ApplicationKey, new string[] {name});
		}

		protected override string GetUserName(Post p)
		{
			GalleryPost gp = p as GalleryPost;
			if(gp == null)
				return base.GetUserName(p);
			else
				return  Globals.HtmlDecode(gp.DisplayName);
		}


	}
}
